package cn.vacuumflask.webviewdemo;

import android.content.Context;
import android.os.Handler;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

//JS 调用接口
public class ContactInfo {

    private Context context;
    private Handler handler;
    private WebView webView;

    public ContactInfo(Context context, WebView webView,Handler handler) {
        this.context = context;
        this.webView = webView;
        this.handler = handler;
    }

    @JavascriptInterface
    public void getNativeMsg(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void nativeMethod() {//JavaBridge 线程

        handler.post(new Runnable() {
            @Override
            public void run() {
                //要在main线程调用
                webView.loadUrl("javascript:alertMsg('互相调用')");
            }
        });

    }

}
